<template>
	<div class="mainsu">
		<div class="main_spac1">
			<div class="main_white padding10 paddingBottom20">
				<div class="main_ti box">
					<el-tabs v-model="activeName" @tab-click="handleClick" class="box">
						<el-tab-pane label="商品列表" name="1"></el-tab-pane>
						<el-tab-pane label="商品分类" name="2"></el-tab-pane>
					</el-tabs>
				</div>

				<!-- 搜索一 -->
				<div
					class="mainor_form"
					style="margin-top: 20px"
					v-show="activeName == 1"
				>
					<div class="mainor_te">
						<el-button type="primary" @click="exportform(1)">导出</el-button>
					</div>
					<el-form
						:inline="true"
						:model="goods_form"
						class="demo-form-inline"
						ref="userListFrom1"
						label-width="70px"
					>
						<el-form-item label="" prop="date_type" class="wid110">
							<el-select v-model="goods_form.date_type" placeholder="请选择">
								<el-option label="配送日期" value="1"> </el-option>
								<el-option label="下单日期" value="2"> </el-option>
							</el-select>
						</el-form-item>
						<el-form-item label="" prop="establishTime">
							<el-date-picker
								v-model="establishTime"
								type="daterange"
								class="time220"
								:editable="false"
								unlink-panels
								range-separator="至"
								start-placeholder="开始日期"
								end-placeholder="结束日期"
								value-format="yyyy-MM-dd"
								:clearable="false"
								:picker-options="pickerOptions"
								@change="dateChange"
							/>
						</el-form-item>
						<div class="more el-form-item" @click="moredata = !moredata">
							<span>更多</span>
							<i :class="[{ on: moredata }, ' el-icon-arrow-down']"></i>
						</div>
						<div class="clearfix"></div>
						<el-collapse-transition>
							<div v-if="moredata" style="width: 100%">
								<div>
									<el-form-item label="商品名称" prop="goods_spu_id">
										<change-goods-input
											ref="changeGoodsInput"
											@clear="
												() => {
													this.goods_form.goods_spu_id = '';
												}
											"
											@sendValue="
												(data) => {
													this.goods_form.goods_spu_id = data.id;
												}
											"
										></change-goods-input>
									</el-form-item>
									<el-form-item label="商品分类" prop="category_id">
										<el-cascader
											v-model="goods_form.category_id"
											:options="options"
											:props="defaultProps"
										></el-cascader>
									</el-form-item>
									<el-form-item label="用户" prop="uid">
										<change-user-input
											ref="changeUserInput"
											@sendValue="getUser"
											@clear="clearUid"
										></change-user-input>
									</el-form-item>
									<el-form-item label="支付方式" prop="is_piroment">
										<el-select
											v-model.trim="goods_form.is_piroment"
											placeholder="支付方式"
											class="wid220"
										>
											<el-option label="所有" value=""></el-option>
											<el-option
												v-for="(is_piroment, index) in is_piroment"
												:key="index"
												:label="is_piroment"
												:value="parseInt(index)"
											></el-option>
										</el-select>
									</el-form-item>
								</div>
							</div>
						</el-collapse-transition>
						<el-form-item>
							<el-button
								class="reset marginLeft10"
								type="primary"
								@click="reset"
								>重置</el-button
							>
							<el-button
								type="primary"
								@click="getArticleList"
								:loading="listLoading"
								>搜索</el-button
							>
						</el-form-item>
					</el-form>
				</div>
				<!-- 搜索二 -->
				<div
					class="mainor_form"
					style="margin-top: 20px"
					v-show="activeName == 2"
				>
					<div class="mainor_te">
						<el-button type="primary" @click="exportform(2)">导出</el-button>
					</div>
					<el-form
						:inline="true"
						:model="form"
						class="demo-form-inline"
						ref="userListFrom2"
						label-width="70px"
					>
						<el-form-item label="" prop="date_type" class="wid110">
							<el-select v-model="form.date_type" placeholder="请选择">
								<el-option label="配送日期" value="1"> </el-option>
								<el-option label="下单日期" value="2"> </el-option>
							</el-select>
						</el-form-item>
						<el-form-item label="" prop="establishTime">
							<el-date-picker
								v-model="establishTime"
								type="daterange"
								class="time220"
								:editable="false"
								unlink-panels
								range-separator="至"
								start-placeholder="开始日期"
								end-placeholder="结束日期"
								value-format="yyyy-MM-dd"
								:clearable="false"
								:picker-options="pickerOptions"
								@change="dateChange"
							/>
						</el-form-item>
						<el-form-item label="用户" prop="uid">
							<change-user-input
								class="time220"
								ref="changeUserInput"
								@sendValue="
									(data) => {
										form.uid = data.info.user_id;
									}
								"
								@clear="form.uid = ''"
							></change-user-input>
						</el-form-item>
						<el-form-item label="支付方式" prop="is_piroment">
							<el-select
								v-model.trim="form.is_piroment"
								placeholder="支付方式"
								class="wid220"
							>
								<el-option label="所有" value=""></el-option>
								<el-option
									v-for="(is_piroment, index) in is_piroment"
									:key="index"
									:label="is_piroment"
									:value="parseInt(index)"
								></el-option>
							</el-select>
						</el-form-item>
						<el-form-item>
							<el-button
								class="reset marginLeft10"
								type="primary"
								@click="reset"
								>重置</el-button
							>
							<el-button
								type="primary"
								@click="getArticleList"
								:loading="listLoading"
								>搜索</el-button
							>
						</el-form-item>
					</el-form>
				</div>
				<!-- 表格一 -->
				<div
					class="main_table marginTop10"
					style="min-height: 550px"
					v-show="activeName == 1"
				>
					<el-table
						ref="multipleTable"
						v-loading="listLoading"
						:data="list"
						element-loading-text="加载中"
						fit
						border
						highlight-current-row
						:header-cell-class-name="this.$verticaline"
					>
						<el-table-column
							label="商品名称"
							prop="goods_name"
							min-width="120"
							show-overflow-tooltip
						>
							<template slot-scope="scope">{{ scope.row.goods_name }}</template>
						</el-table-column>

						<el-table-column
							label="SKU编码"
							prop="goods_spu_code"
							show-overflow-tooltip
						/>

						<el-table-column label="单位" prop="unit" show-overflow-tooltip />
						<el-table-column
							label="订单数量"
							prop="order_num"
							show-overflow-tooltip
						>
						</el-table-column>
						<el-table-column
							label="配送数量"
							prop="send_num"
							show-overflow-tooltip
						>
						</el-table-column>
						<el-table-column label="配送金额" prop="send_money" min-width="120">
							<template slot="header">
								<headerSortArrow
									label="send_money"
									ref="arrow1"
									@handleSort="handleSort($event, 'arrow1')"
									>配送金额</headerSortArrow
								>
							</template>
							<template slot-scope="scope">
								{{ scope.row.send_money | twoPlace }}
							</template>
						</el-table-column>
						<el-table-column label="退单数量" prop="return_order_num">
						</el-table-column>
						<el-table-column label="退货数量" prop="return_num">
						</el-table-column>
						<el-table-column
							label="退货金额"
							prop="return_money"
							min-width="120"
						>
							<template slot="header">
								<headerSortArrow
									label="return_money"
									ref="arrow2"
									@handleSort="handleSort($event, 'arrow2')"
									>退货金额</headerSortArrow
								></template
							>
							<template slot-scope="scope">
								{{ scope.row.return_money | twoPlace }}
							</template>
						</el-table-column>
						<el-table-column
							label="管理操作"
							min-width="140"
							prop=""
							fixed="right"
						>
							<template slot-scope="scope">
								<el-link type="primary" @click="dayReport(scope.row, 1)"
									>日报表</el-link
								>
								<el-link type="primary" @click="dayReport(scope.row, 2)"
									>用户</el-link
								>
							</template>
						</el-table-column>
					</el-table>
				</div>
				<!-- 表格二 -->
				<div
					class="main_table marginTop10"
					style="min-height: 550px"
					v-show="activeName == 2"
					v-cloak
				>
					<el-table
						ref="multipleTable"
						v-loading="listLoading"
						:data="list"
						element-loading-text="加载中"
						fit
						border
						highlight-current-row
						:default-sort="{ prop: 'send_money' }"
						:header-cell-class-name="this.$verticaline"
					>
						<el-table-column label="一级分类" prop="category_name">
							<template slot-scope="scope">{{
								scope.row.category_name
							}}</template>
						</el-table-column>

						<el-table-column label="配送金额" prop="send_money" />
					</el-table>
				</div>
				<div class="main_page boxdq">
					<div></div>
					<!-- 分页 -->
					<div class="main_page_r">
						<el-pagination
							:current-page="current.page"
							:page-sizes="[20, 30, 50, 100]"
							:page-size="limit"
							layout="total, sizes, prev, pager, next, jumper"
							:total="totalCount"
							background
							@size-change="
								(page) => {
									changeLimit(page, 1);
								}
							"
							@current-change="
								(page) => {
									changePage(page, 1);
								}
							"
						/>
					</div>
				</div>
			</div>
		</div>

		<!-- 日报表 和  用户 -->
		<el-drawer
			class="report_drawer drawer_min900"
			:title="drawer_header_title"
			size="50%"
			:visible.sync="goods_drawer"
			direction="rtl"
			@closed="drawerClose"
		>
			<div class="drawer_container">
				<div class="drawer_container_header">
					<div class="heder_left">
						<span>{{ drawer_title }}</span>
					</div>
					<div class="header_right">
						<el-button type="primary" @click="exportform(3)">导出</el-button>
					</div>
				</div>
				<div class="drawer_container_main">
					<div class="main_table marginTop10" style="min-height: 400px">
						<el-table
							ref="multipleTable"
							v-loading="drawer_loading"
							:data="drawer_list"
							element-loading-text="加载中"
							fit
							border
							highlight-current-row
							:header-cell-class-name="this.$verticaline"
						>
							<el-table-column
								:label="drawer_type == 1 ? '配送日期' : '用户名称'"
								:prop="drawer_type == 1 ? 'day' : 'nickname'"
							/>

							<el-table-column label="配送数量" prop="send_num" />
							<el-table-column label="配送金额" prop="send_money">
								<template slot-scope="scope">
									{{ scope.row.send_money | twoPlace }}
								</template>
							</el-table-column>

							<el-table-column label="退货数量" prop="return_num" />
							<el-table-column label="退货金额" prop="return_money">
								<template slot-scope="scope">
									{{ scope.row.return_money | twoPlace }}
								</template>
							</el-table-column>
						</el-table>
					</div>
					<div class="main_page boxdq">
						<div></div>
						<!-- 分页 -->
						<div class="main_page_r">
							<el-pagination
								:current-page="drawer_current.page"
								:page-sizes="[20, 30, 50, 100]"
								:page-size="drawer_limit"
								layout="total, sizes, prev, pager, next, jumper"
								:total="drawer_totalCount"
								background
								@size-change="
									(page) => {
										changeLimit(page, 2);
									}
								"
								@current-change="
									(page) => {
										changePage(page, 2);
									}
								"
							/>
						</div>
					</div>
				</div>
			</div>
		</el-drawer>
	</div>
</template>

<script>
	import {
		get_saleGoods,
		get_saleCategory,
		get_saleGoodsDetail,
		get_saleGoodsMemberDetail,
		export_exportSaleGoods,
		export_exportSaleGoodsDetail,
		export_exportSaleGoodsMemberDet,
		export_exportSaleCategory,
		get_report_base,
	} from "@/api/report/index";
	import { apiUrl, objKeySort, Md5 } from "@/utils/public";
	import { report_shortcuts } from "@/options/new_dateOption";
	import { parseTime, exportMethod, getNowMonth } from "@/utils/index";
	import { recursion, category } from "@/utils/index";
	import headerSortArrow from "@/views/report/components/headerSortArrow";

	export default {
		components: { headerSortArrow },
		data() {
			return {
				pickerOptions: report_shortcuts,
				exportMethod,
				// 时间
				parseTime,
				getNowMonth,
				// 分页
				current: {
					page: 1,
					per_page: 20,
				},
				limit: 20,
				totalCount: 1,
				// 列表数据
				list: [],
				listLoading: false,
				// 分类搜索
				form: {
					start_time: "",
					end_time: "",
					uid: "",
					is_piroment: "",
					date_type: "1",
				},
				// 商品搜索
				goods_form: {
					start_time: "",
					end_time: "",
					category_id: "",
					goods_spu_id: "",
					uid: "",
					is_piroment: "",
					date_type: "1",
					order_by: "",
					order_type: "", //1升序  2降序
				},
				is_piroment: {},
				// 分类
				options: [],
				defaultProps: {
					children: "selfNext",
					label: "title",
					value: "id",
					checkStrictly: true,
				},
				establishTime: [],
				activeName: "1",
				moredata: false,
				goods_drawer: false, //商品明细抽屉
				drawer_current: {
					page: 1,
					per_page: 20,
				},
				drawer_limit: 20,
				drawer_totalCount: 1,
				drawer_list: [],
				drawer_loading: false,
				drawer_title: "商品名称",
				drawer_header_title: "每日报表",
				drawer_type: 1,
				drawer_form: {
					start_time: "",
					end_time: "",
					goods_spu_id: "",
				},
			};
		},
		watch: {
			establishTime: {
				handler(val) {
					this.goods_form.start_time = val[0] ? val[0] : "";
					this.goods_form.end_time = val[1] ? val[1] : "";
					this.form.start_time = val[0] ? val[0] : "";
					this.form.end_time = val[1] ? val[1] : "";
				},
			},
		},
		created() {
			// 列表
			this.establishTime = this.getNowMonth();
			this.get_report_base();
			this.get_sales_list();
		},
		methods: {
			// 基本信息
			get_report_base() {
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
						),
					},
					apiUrl().name
				);
				get_report_base(data).then((res) => {
					if (res.errcode == 0) {
						this.is_piroment = res.data.is_piroment;
					}
				});
			},
			// 获取用户
			getUser(data) {
				let uid = data.info.user_id;
				this.goods_form.uid = uid;
			},
			// 清除用户的uid
			clearUid() {
				this.goods_form.uid = "";
			},
			get_sales_list() {
				let func = "";
				switch (this.activeName) {
					case "1":
						func = get_saleGoods;
						break;
					case "2":
						func = get_saleCategory;
						break;
				}
				this.listLoading = true;
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
						),
					},
					apiUrl().name,
					this.list_form(),
					this.current
				);
				func(data).then((res) => {
					if (res.errcode == 0) {
						this.list = res.data.list;
						this.totalCount = res.data.total;
						if (res.data.category_list) {
							this.options = recursion(res.data.category_list, true);
						}
					} else {
						this.$message.error(res.errmsg);
					}
					this.listLoading = false;
				});
			},

			// 重置
			reset() {
				let ref = `userListFrom${this.activeName}`;
				this.$refs[ref].resetFields();
				this.establishTime = this.getNowMonth();
				if (this.moredata && this.activeName == "1") {
					this.$refs["changeGoodsInput"].name = "";
					this.$refs["changeUserInput"].name = "";
				}
				if (this.$refs["changeUserInput"]) {
					this.$refs["changeUserInput"].name = "";
				}
				this.get_sales_list();
			},
			// 搜索
			getArticleList() {
				this.current.page = 1;
				this.get_sales_list();
			},
			// 分页
			changeLimit(pageSize, type) {
				switch (type) {
					case 1:
						this.current.per_page = pageSize;
						this.get_sales_list();
						break;
					case 2:
						this.drawer_current.per_page = pageSize;
						if (this.drawer_type == 1) {
							this.get_saleGoodsDetail();
						} else if (this.drawer_type == 2) {
							this.get_saleGoodsMemberDetail();
						}
						break;
				}
			},
			changePage(page, type) {
				switch (type) {
					case 1:
						this.current.page = page;
						this.get_sales_list();
						break;
					case 2:
						this.drawer_current.page = page;
						if (this.drawer_type == 1) {
							this.get_saleGoodsDetail();
						} else if (this.drawer_type == 2) {
							this.get_saleGoodsMemberDetail();
						}

						break;
				}
			},

			list_form() {
				let form = null;
				switch (this.activeName) {
					case "1":
						form = JSON.parse(JSON.stringify(this.goods_form));
						form.pid = form.category_id[0] ? form.category_id[0] : "";
						form.category_id = category(form.category_id);
						break;
					case "2":
						form = JSON.parse(JSON.stringify(this.form));
						break;
				}
				form.start_time = this.establishTime[0] ? this.establishTime[0] : "";
				form.end_time = this.establishTime[1] ? this.establishTime[1] : "";
				return form;
			},
			// 排序
			handleSort(data, ref) {
				let arr = ["arrow1", "arrow2"].filter((e) => e != ref);
				arr.map((e) => {
					this.$refs[e].selected = false;
				});
				this.goods_form.order_by = data.label;
				this.goods_form.order_type = data.type;
				this.get_sales_list();
			},
			handleClick(tab, event) {
				this.list = [];
				if (this.activeName == 1) {
					this.current.page = 1;
					this.get_sales_list();
				} else if (this.activeName == 2) {
					this.current.page = 1;
					this.get_sales_list();
				}
			},
			// 导出
			exportform(type) {
				switch (type) {
					case 1:
						this.export_exportSaleGoods();
						break;
					case 2:
						this.export_exportSaleCategory();
						break;
					case 3:
						if (this.drawer_type == 1) {
							this.export_exportSaleGoodsDetail();
						} else if (this.drawer_type == 2) {
							this.export_exportSaleGoodsMemberDet();
						}
						break;
				}
			},
			// 商品导出
			export_exportSaleGoods() {
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
						),
					},
					apiUrl().name,
					this.goods_form
				);
				export_exportSaleGoods(data).then((res) => {
					this.exportMethod(res, "销售报表-商品");
				});
			},
			// 商品日报表导出
			export_exportSaleGoodsDetail() {
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
						),
					},
					apiUrl().name,
					this.drawer_form
				);
				export_exportSaleGoodsDetail(data).then((res) => {
					this.exportMethod(res, "销售报表-商品-日报表");
				});
			},
			// 商品用户导出
			export_exportSaleGoodsMemberDet() {
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
						),
					},
					apiUrl().name,
					this.drawer_form
				);
				export_exportSaleGoodsMemberDet(data).then((res) => {
					this.exportMethod(res, "销售报表-商品-用户");
				});
			},
			// 分类导出
			export_exportSaleCategory() {
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
						),
					},
					apiUrl().name,
					this.form
				);
				export_exportSaleCategory(data).then((res) => {
					this.exportMethod(res, "销售报表-分类");
				});
			},
			// 日期选择
			dateChange(val) {
				let start = new Date(val[0]).getTime();
				let end = new Date(val[1]).getTime();
				let diff = start - end;
				if (Math.abs(diff) > 3600 * 1000 * 24 * 60) {
					this.$message.warning("选择时间跨度不能超过两个月");
					this.establishTime = this.getNowMonth();
				}
				this.get_sales_list();
			},
			// 日报表 和 用户报表
			dayReport(row, type) {
				this.drawer_type = type;
				switch (type) {
					case 1:
						this.drawer_header_title = "每日报表";
						break;
					case 2:
						this.drawer_header_title = "用户报表";
						break;
				}
				this.drawer_title = row.goods_name;
				this.drawer_form = {
					start_time: "",
					end_time: "",
					goods_spu_id: row.goods_spu_id,
					is_piroment: this.goods_form.is_piroment,
					date_type: this.goods_form.date_type,
				};
				if (this.establishTime) {
					this.drawer_form.start_time = this.establishTime[0]
						? this.establishTime[0]
						: "";
					this.drawer_form.end_time = this.establishTime[1]
						? this.establishTime[1]
						: "";
				}
				switch (type) {
					case 1:
						this.get_saleGoodsDetail();
						break;
					case 2:
						this.get_saleGoodsMemberDetail();
						break;
				}
				this.goods_drawer = true;
			},
			// 每日明细
			get_saleGoodsDetail() {
				this.drawer_loading = true;
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
						),
					},
					apiUrl().name,
					this.drawer_form,
					this.drawer_current
				);
				get_saleGoodsDetail(data).then((res) => {
					if (res.errcode == 0) {
						this.drawer_list = res.data.list;
						this.drawer_totalCount = res.data.total;
						this.drawer_loading = false;
					} else {
						this.$message.warning(res.errmsg);
					}
				});
			},
			// 用户明细
			get_saleGoodsMemberDetail() {
				this.drawer_loading = true;
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
						),
					},
					apiUrl().name,
					this.drawer_form,
					this.drawer_current
				);
				get_saleGoodsMemberDetail(data).then((res) => {
					if (res.errcode == 0) {
						console.log(res);
						this.drawer_list = res.data.list;
						this.drawer_totalCount = res.data.total;
						this.drawer_loading = false;
					} else {
						this.$message.warning(res.errmsg);
					}
				});
			},
			drawerClose() {
				this.drawer_list = [];
				this.drawer_current.page = 1;
				this.drawer_current.per_page = 20;
				this.drawer_limit = 20;
				this.drawer_form = {
					start_time: "",
					end_time: "",
					goods_spu_id: "",
					is_piroment: "",
				};
			},
		},
	};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
	.mainor_form form {
		width: calc(100% - 180px);
	}
}

// 支付状态
.main_ti {
	display: flex;
	align-items: center;
	flex-direction: row;
	margin-bottom: 15px;
	padding-bottom: 1px;
	position: relative;
	.back {
		padding: 0 55px 0 0;
		font-size: 14px;
		color: #03a98e;
		display: flex;
		align-items: center;
		flex-direction: row;
		font-size: 0;
		a {
			font-size: 14px;
			color: #03a98e;
		}
		span {
			font-size: 14px;
			padding: 0 14px;
			transform: translateY(-1px);
		}
	}
	/deep/.el-tabs__header {
		margin: 0;
	}
	/deep/.el-checkbox {
		margin-top: 12px;
	}
	&::after {
		height: 1px;
		content: "";
		position: absolute;
		left: 0;
		bottom: 0;
		width: 100%;
		background-color: #e4e7ed;
		z-index: 1;
	}
	/deep/.el-tabs__nav-wrap::after {
		height: 0;
	}
}
</style>
